using System;
using System.Collections.Generic;
using System.Text;
using JobShopModel.Model.FactoryLvl;
using JobShopModel.Model.Station;
using JobShopModel.Model.JobLvl;
using JobShopModel.Model;

namespace JobShopModel.Events
{
    /// <summary>
    /// Event that is raised when the transporter removes a job that was blocking
    /// a station's machine
    /// </summary>
    public class UnBlockStation : TransportEvent
    {

        private RegularStation currentStation;
        private Job currentJob;

        public UnBlockStation(Time stime, StationsName name,Job jobDone)
            : base(stime)
        {
            this.currentStation = (RegularStation)Factory.getInstance().Stations[name];
            this.currentJob = jobDone;
        }
        
        public override void Process()
        {
            Factory fact = Factory.getInstance();
            Time elapsedTime = new Time();

            /* 
            * Take the Job from the station,
            * set the Machine that performed the operation to Iddle (End the blockedTime)
            */
            Operation currentOperation = currentJob.CurrentOperation;
            Machine operatingMachine = currentStation.getMachine(currentOperation);
            //Store the time waiting in the FIFO list of the transporter
            currentJob.EndWaitingFIFO(fact.CurrentTime);

            //Store the BlockedTime for that operation
            currentOperation.setDone(fact.CurrentTime);

            //Set the machine ot Iddle (get the Blocked Time)
            //HACKS !!
            //if (operatingMachine != null)
            Time blocked = operatingMachine.SetIddle(fact.CurrentTime);

            //Update statistics
            if (fact.CurrentTime.Hours != (Decimal)0 && fact.CurrentTime.Days != 0)
            {
                currentStation.TotalBlockedTime = blocked;
                currentStation.AvgBlockedTime = currentStation.TotalBlockedTime / fact.CurrentTime;
            }
            //Get the station back to work
            PerformOperation nextEvent = new PerformOperation(fact.CurrentTime, currentStation.Name);
            fact.EventQueue.insertEvent(nextEvent);
            Logger.InsertElement(new LogItem(ItemPriorities.SLOW, fact.CurrentTime, currentStation + ", Looking for another Job in the Queue"));
            Logger.InsertElement(new LogItem(ItemPriorities.CREATION, fact.CurrentTime, currentStation + ", insterted: \"" + nextEvent.ToString() + "\""));

            //Select the next Operation to be done.
            currentJob.SelectNextOperation();

                    //Get the next destination from the newly set operation
                    StationsName destination = currentJob.CurrentOperation.Station;

                    //Transport to station
                    Time transportTime = Transporter.TravelTo(destination);
                    Logger.InsertElement(new LogItem(ItemPriorities.SLOW,fact.CurrentTime,"Transporting to station "+destination+"..."));

                    //Update time spent
                    elapsedTime += transportTime;

                    //Call EndTransport Event to set transporter IDDLE at the correct time.
                    EndTransport secondEvent = new EndTransport(fact.CurrentTime + elapsedTime, destination, currentJob);
                    fact.EventQueue.insertEvent(secondEvent);
                    Logger.InsertElement(new LogItem(ItemPriorities.CREATION, fact.CurrentTime, "The Transporter inserted: \"" + secondEvent.ToString() + "\""));
          

           



        }

        public override string ToString()
        {
            return "UnBlock_Station ("+ExecTime+")";
        }
    }
}
